package com.zxs.category.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.zxs.category.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import parm.PageParam;
import parm.ProductHotParam;
import pojo.Category;
import utils.R;

import java.util.List;

/**
 * @author zxs
 * *7 18:13
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/promo/{categoryName}")
    public R byName(@PathVariable String categoryName){

        if (StringUtils.isEmpty(categoryName)){
            return R.fail("类别名称为null,无法查询类别数据!");
        }

        return categoryService.byName(categoryName);
    }
    @PostMapping("hots")
    public R hots(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("类别集合不能为空！查询失败！");
        }
        return categoryService.hots(productHotParam);
    }
    @GetMapping("list")
    public R list(){
        return  categoryService.list();
    }

    /**
     * 后台商品编辑界面所需要
     * @return
     */
    @GetMapping("admin/list")
    public List<Category> adminlist(){
        return  categoryService.adminlist();
    }



    /**
     * 分类列表
     * @param pageParam
     * @return
     */
    @PostMapping("admin/list")
    public  R categoryList(@RequestBody PageParam pageParam){
        return categoryService.categoryList(pageParam);
    }
    @PostMapping("admin/save")
    public R categorySave(@RequestBody Category category){
        return  categoryService.categorySave(category);
    }

    @PostMapping("admin/remove")
    public R categoryRemove(@RequestBody Category category){
        return  categoryService.categoryRemove(category);
    }


    @PostMapping("admin/update")
    public R categoryUpdate(@RequestBody Category category){
        return categoryService.categoryUpdate(category);
    }


}
